package com.jinxiangyang.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinxiangyang.annotation.OperateLog;
import com.jinxiangyang.domain.UserLoginLog;
import com.jinxiangyang.service.UserLoginLogService;
import com.jinxiangyang.utils.StringUtils;
import com.jinxiangyang.utils.enums.BusinessType;
import com.jinxiangyang.utils.enums.OperatorType;
import com.jinxiangyang.utils.result.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户登陆记录表 前端控制器
 * </p>
 *
 * @author jinxiangyang
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/userLogin")
public class UserLoginLogController {


    @Autowired
    private UserLoginLogService userLoginLogService;



    @GetMapping("/{pageNumber}/{pageSize}")
    @OperateLog(title = "登录日志",businessType = BusinessType.OTHER,operatorType = OperatorType.ADMIN)
    public AjaxResult getUserLoginLog(@PathVariable Long pageNumber, @PathVariable Long pageSize, UserLoginLog userLoginLog){
        Page<UserLoginLog> userLoginLogPage = new Page<>(pageNumber,pageSize);
        LambdaQueryWrapper<UserLoginLog> userLoginLogLambdaQueryWrapper=new  LambdaQueryWrapper<UserLoginLog>();
        userLoginLogLambdaQueryWrapper.like(StringUtils.isNotNull(userLoginLog.getUserName()),UserLoginLog::getUserName,userLoginLog.getUserName());
        userLoginLogLambdaQueryWrapper.like(StringUtils.isNotNull(userLoginLog.getCity()),UserLoginLog::getCity,userLoginLog.getCity());
        userLoginLogLambdaQueryWrapper.like(StringUtils.isNotNull(userLoginLog.getMsg()),UserLoginLog::getMsg,userLoginLog.getMsg());
        Page<UserLoginLog> page = userLoginLogService.page(userLoginLogPage, userLoginLogLambdaQueryWrapper);
        if(pageNumber>page.getPages()){
            page=userLoginLogService.page(new Page<>(userLoginLogPage.getPages(),pageSize),userLoginLogLambdaQueryWrapper);
        }
        return AjaxResult.success("查询成功",page);
    }


    @DeleteMapping
    public  AjaxResult delUserLoginLog(){
        return userLoginLogService.cleaner()?AjaxResult.success():AjaxResult.error("清空成功");
    }





}

